﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EventReminder.Common;
using System.Security.Authentication;

namespace EventReminder.Server
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class EventReminderService : IEventReminderService
    {
        UsersManager users = new UsersManager();

        public EventReminderService()
        {
        }

        public string Login(string userName, string password)
        {            

            string returnValue = null;

            bool check = users.ValidateUser(userName,password);
            if (check)
            {
                string sessionId = Guid.NewGuid().ToString(); 
                returnValue = sessionId;
                users.SetSessionID(userName, sessionId);
            }
            else
            {
                returnValue = ReturnCodes.INVALID_USERNAME_OR_PW;
            }

            return returnValue;
        }

        public string Register(string userName, string password, string firstName, string lastName, string email)
        {
            // TODO (Les): fill with actual register logic from EventReminder.Server

            string returnValue = null;
            // attempt to register logic (ie., verify unique userName, valid username and passWord)
            //  Use Common.PasswordValidator to validate userName and password (default settings)

            // login if registration successful
            string sessionId = Guid.NewGuid().ToString(); // store sessionId somewhere
            returnValue = sessionId;

            // return error code if unsuccessful  // TODO: replace with exception (can't) or event
            // 1. username exists       // TODO: figure out how to raise events accross wire
            //return lowercase data
            returnValue = ReturnCodes.USERNAME_IN_USE;
            // 2. username is invalid
            // 3. password is invalid
            returnValue = ReturnCodes.INVALID_USERNAME_OR_PW;

            return returnValue;
        }

        public IEnumerable<EventData> GetEvents(DateTime day, string sessionId)
        {
            // TODO (Les): Delete all of this and replace with actual event-fetching logic from EventReminder.Server
            List<EventData> testEvents = new List<EventData>(); // empty list for no events that day

#if DEBUG
            EventData testEvent = new EventData();
            testEvent.Date = DateTime.Now;
            testEvent.Description = "Attend to task";
            testEvent.Emails = new string[] { "a@b.com", "b@b.com" };
            testEvent.FollowUp = false;
            testEvent.ID = 279;
            testEvent.LastReminderSent = DateTime.Now.AddDays(-1);
            testEvent.NextReminderDue = DateTime.Now.AddDays(1);
            testEvent.Reminder = "Get this done!";
            testEvent.Title = "The Big Event";
            testEvent.Type = EventTypes.Event;
            testEvents.Add(testEvent); 
#endif

            return testEvents;
        }

        public void AddEvent(EventData e, string sessionId)
        {
            // TODO (Les): fill with actual Add Event logic from EventReminder.Server

        }

        public void ModifyEvent(EventData old, EventData update, string sessionId)
        {
            // TODO (Les): fill with actual Modify Event logic from EventReminder.Server
        }

        public void DeleteEvent(EventData e, string sessionId)
        {
            // TODO (Les): fill with actual Delete Event logic from EventReminder.Server
        }

        public void Logout(string sessionId)
        {
            // process logout
        }
    }
}
